<template>
  <!--会签管理-我的提交列表-项目合同审批详情弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container class="scroll-view" style="height: 600px">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="审批编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvNumber" style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.approvNumber" placeholder="请输入介绍"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="提交时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="submitTime" style="margin-bottom: 10px;">
              <j-date
                v-model="model.submitTime"
                :showTime="true"
                 :disabled="disabled"
                date-format="YYYY-MM-DD "
                style="width: 100%"
                placeholder="请选择提交时间"
              ></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="发起人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userName" style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.userName" placeholder="请输入发起人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="post" style="margin-bottom: 10px;">
              <a-select :disabled="disabled" placeholder="请选择职位" v-model="model.post">
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName" style="margin-bottom: 10px;">
              <a-select :disabled="disabled" :options="departList" placeholder="请选择申请部门" v-model="model.departName">
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status" style="margin-bottom: 10px;">
              <a-select :disabled="disabled" placeholder="请选择状态" v-model="model.status">
                <a-select-option value="0">未提交</a-select-option>
                <a-select-option value="1">审核中</a-select-option>
                <a-select-option value="2">已同意</a-select-option>
                <a-select-option value="3">已驳回</a-select-option>
                <a-select-option value="4">已撤销</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime"
              style="margin-bottom: 10px;">
              <a-date-picker :disabled="disabled" placeholder="请选择开始日期" format="YYYY-MM-DD" v-model="model.startTime" style="width:100%;" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="申请部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departName"
              style="margin-bottom: 10px;">
              <a-select :disabled="disabled" :options="departList" placeholder="请选择申请部门" v-model="model.departName">
              </a-select>
            </a-form-model-item>
          </a-col> -->
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId"
              style="margin-bottom: 10px;">
              <a-select :disabled="disabled" :options="levelOneOptions" placeholder="请选择项目" v-model="model.projectId">
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.name" placeholder="请输入合同名称"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deadline"
              style="margin-bottom: 10px;">
              <div style="display: flex;align-items: center;">
                <a-input style="width: calc(100% - 40px);" :disabled="disabled" v-model="model.deadline" placeholder="请输入合同期限"></a-input><div style="width: 40px;">个月</div>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.number" placeholder="请输入合同份数"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.other" placeholder="请输入对方单位名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.money" placeholder="请输入合同金额"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherAddress"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.otherAddress" placeholder="请输入对方单位地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractNumber"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.contractNumber" placeholder="请输入合同编号"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="对方单位电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherPhone"
              style="margin-bottom: 10px;">
              <a-input :disabled="disabled" v-model="model.otherPhone" placeholder="请输入对方单位电话"></a-input>
            </a-form-model-item>
          </a-col>
         
        </a-row>
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="合同备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;">
              <textarea :disabled="disabled" v-model="model.remark" cols="100" rows="3" style="width: 119%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="合同协议附件资料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment"
              style="margin-bottom: 10px;">
              <j-upload :disabled="disabled" v-model="model.attachment" :buttonVisible="!disabled"></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="pageType === 'add'">
            <div style="display: flex;justify-content: space-between;align-items: center;margin-right: 40px;">
              <div style="line-height: 20px;color: red;">1.请附会签完成的询价单；<br/>2.请附对方营业执照、资质证书等相关材料。</div>
               <a-button @click="downfile" type="primary">下载合同模板</a-button>
            </div>
          </a-col>
        </a-row>
         <a-row>
           <a-form-model-item label="审核流程" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
             style="margin-bottom: 10px;">
             <div style="display: flex;flex-direction: column;width: 119%;">
               <block v-for="(item, index) in list" :key="index">
                 <div style="display: flex;flex-direction: column;">
                   <div style="display: flex;align-items: center;">
                     <div style="width:60px;margin-right: 5px;">审批人{{index + 1 }} </div>
                     <div
                       style="border: 0.5px solid gray; width: 599px; padding: 0 5px;display: flex;align-items: center;" :style="{background:((item.approver === approver)&&(item.status === '1')) && pageType ==='approver'?'#fff':'#f5f5f5'}">{{ item.departName }} {{ item.realname }} 意见:<a-input :disabled="((item.approver === approver)&&(item.status === '1')) && pageType ==='approver'?false:true " v-model="item.feedback" placeholder="" style="width: 400px;border: none;background: none;"></a-input></div>
                     <a-list placeholder="请选择状态" :data-source="list"
                       style="font-size: 16px; width: 80px; margin-left: 5px; display: inline-block">
                       <!-- <a-list-item v-if="item.status == '0'" style="color: blue">待审核</a-list-item> -->
                       <a-list-item v-if="item.status == '1'" style="color: blue">审核中</a-list-item>
                       <a-list-item v-if="item.status == '2'" style="color: green">已同意</a-list-item>
                       <a-list-item v-if="item.status == '3'" style="color: red">已驳回</a-list-item>
                       <a-list-item v-if="item.status == '4'" style="color: gray">已撤销</a-list-item>
                     </a-list>
                   </div>
                   <div style="margin-left: 65px;margin-bottom: 10px;">
                     <j-upload v-model="item.ideaAttachment" :disabled="((item.approver === approver)&&(item.status === '1')&&(item.isAttachment === '1')) && pageType ==='approver'?false:true" :buttonVisible="!(((item.approver === approver)&&(item.status === '1')&&(item.isAttachment === '1')) && pageType ==='approver'?false:true )"></j-upload>
                   </div>
                 </div>
               </block>
             </div>
           </a-form-model-item>
        </a-row>
         <a-row>
           <a-button v-if="!(pageType === 'approver'?false:true)" style="background: orange;color: white;border:1px solid orange;margin-left: 118px;" @click="handleOpen('history')">查看原审核记录</a-button>
        </a-row>
      </a-form-model>
    </j-form-container>
    <j-modal class="modal-privacy" :title="title" :visible="visible1" :disableScreenClick="true" okText="确定"
      cancelText="取消" @cancel="cancelPrivacy" @ok="confirmPrivacy">
      <textarea cols="70" rows="5" placeholder="请输入撤销原因"></textarea>

      <!-- <div style="font-size: 20px;width: 200px;height: 20px;margin: 0 auto;margin-top: 10px;">确定提交吗？</div> -->
    </j-modal>
    <wt-shishi-modal ref="modalForm"></wt-shishi-modal>

  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'
  import WtShishiModal from './WtShishiModal'

  export default {
    name: 'WtBrandForm',
    components: {
      WtShishiModal
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: true,
        required: false,
      },
      defaultModel:{
        type: Object,
        default(){
        	return{
        	}
        }
      }
    },
    data() {
      return {
        title: '撤销原因',
        model: {},
        visible1: false,
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 14
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        confirmLoading: false,
        validatorRules: {},
        levelOneOptions: [],
        departList:[],
        // levelTwoOptions: [],
        // levelThreeOptions: [],
        treeData: [],
        treeData1: [],
        treeData2: [],

        list: [],
        // fileList: [],
        url: {
          add: '/wtBrand/add',
          edit: '',
          approver:'/countersign/wtCountersignProcess/submitExamineApprove',
          queryById: '/wtBrand/queryById',
        },
        approver:'',
        pageType:'',
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
      
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      if(!!window.localStorage.getItem("pro__USER_DEPARTLIST")){
        let departList = JSON.parse(window.localStorage.getItem("pro__USER_DEPARTLIST")).value;
        if(departList && departList.length>0){
          this.departList = departList.map((ite)=>({
            ...ite,
            value:ite.id,
            label:ite.departName
          }))
        }
      }
      if(!!window.localStorage.getItem("pro__Login_Userinfo")){
        let userInfo = JSON.parse(window.localStorage.getItem("pro__Login_Userinfo")).value;
        this.approver = userInfo.id;
        console.log(33,this.approver)
      }
    },
    mounted() {
      if(!!this.defaultModel.id){
        this.model = JSON.parse(JSON.stringify(this.defaultModel));
        this.list = this.model.list;
      }
    },
    methods: {
      confirmPrivacy() {
        // console.log('同意了用户协议')
        // console.log(this.privacyDialogShow)l
        this.visible1 = false
        // console.log(this.privacyDialogShow)
      },
      cancelPrivacy() {
        // console.log('拒绝了用户协议')
        this.visible1 = false
      },
      submitForm1: function(record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "新增";
        // this.$refs.modalForm.disableSubmit = true;
      },
      loadTree(record) {
        var that = this;
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.treeData = []
            let treeList = res.result
            for (let a = 0; a < treeList.length; a++) {
              let temp = treeList[a]
              temp.isLeaf = temp.leaf
              this.treeData.push(temp)
            }
        
            this.levelOneOptions = [...this.treeData].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
            getAction(`/countersign/wtCountersignProjectContract/queryByCountersignId?countersignId=${record.id}`).then(
              (res) => {
                if (res.success) {
                  this.model = res.result;
                  this.list = res.result.list;
                  if(this.levelOneOptions && this.levelOneOptions.length>0){
                    let info = this.levelOneOptions.find((item) => item.departName == this.model.projectName);
                    this.model.projectId = info.id;
                  }
                  if(this.departList && this.departList.length>0){
                    let info2 = this.departList.find((item) => item.departName == this.model.departName);
                    if(!!info2){this.model.depart_id = info2.id;}
                  }
                  this.$forceUpdate();
                }
              }
            )
          }
        })
      },
      add() {
        this.edit(this.modelDefault)
      },
      edit(record,type) {
        // this.model = Object.assign({}, record)
        if(!!type){
          this.pageType = type;
        }
        this.visible = true
        this.loadTree(record)
      },
      chexiao() {
        this.visible1 = true
      },
      //查看原审核记录
      handleOpen(type){
        this.$emit('handleOpen',type);
      },
      submitForm(record) {
        const that = this
        
        let httpurl = '';
        let method = '';
        let params = {};
        if (this.pageType === 'approver') { //驳回/同意-审批
          httpurl = this.url.approver;
          method = 'POST';
          params.status = record;
          if(this.list && this.list.length>0){
            this.list.forEach(ite=>{
              if(this.approver === ite.approver && ite.status === '1'){
                params.feedback = ite.feedback;
                params.ideaAttachment = ite.ideaAttachment;
                params.id = ite.id;
              }
            })
          }
        }
        console.log('参数',params,this.approver)
        if (!params.feedback) {
          that.$message.warning('审批意见不能为空,请填写')
        } else {
          that.confirmLoading = true
          httpAction(httpurl, params, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
        
      },
    },
  }
</script>
<style scoped>
  .scroll-view {
    overflow: auto;
    overflow-y: scroll;
  }
</style>